package com.pucheng.bracelet.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.pucheng.bracelet.app.util.DateFormat
import com.pucheng.bracelet.databinding.ItemListClockBinding
import com.pucheng.bracelet.app.util.DatetimeUtil
import java.io.Serializable

class ClockAdapter(
    private val callback: (position: Int, data: ClockData) -> Unit,
    private val callbackFragment: (position: Int, data: ClockData) -> Unit
) :
    ListAdapter<ClockData, ClockHolder>(object : ItemCallback<ClockData>() {
        override fun areItemsTheSame(oldItem: ClockData, newItem: ClockData): Boolean =
            oldItem.clockTime == newItem.clockTime

        override fun areContentsTheSame(oldItem: ClockData, newItem: ClockData): Boolean =
            oldItem == newItem

    }) {
    private var datas: MutableList<ClockData>? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ClockHolder {
        return ClockHolder(
            ItemListClockBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            ),
            {
                it?.apply {
                    callback.invoke(this, datas!![this])
                }
            },
            {
                it?.apply {
                    callbackFragment.invoke(this, datas!![this])

                }
            }
        )
    }

    override fun onBindViewHolder(holder: ClockHolder, position: Int) {
        val item = getItem(position)
        item?.apply {
            holder.bind(this, position)
        }
    }

    override fun submitList(list: MutableList<ClockData>?) {
        datas = list
        super.submitList(datas)
    }
}

class ClockHolder(
    private val binding: ItemListClockBinding,
    callback: (position: Int?) -> Unit,
    callbackFragment: (position: Int?) -> Unit
) :
    RecyclerView.ViewHolder(binding.root) {
    private var position: Int? = 0
    private var clockData: ClockData? = null

    init {
        binding.setOnSelectClickListener {
            callbackFragment.invoke(position)
        }
        binding.setOnCheckListener {
            clockData?.apply {
                clockData?.selected = !clockData?.selected!!
                bind(clockData, position)
                callback.invoke(position)
            }
        }
//        myCallback = callback
    }

    fun bind(clockData: ClockData?, position: Int?) {
        this.position = position
        this.clockData = clockData
        binding.apply {
            clock = clockData
            tvClock.text = DateFormat.timeFormat(clockData!!.clockTime)
            tvClockTips.text = DatetimeUtil.getWeekRepeatData(
                weekChecks = clockData.weekRepeat
            )
            sbClock.isChecked = clockData?.selected ?: false

            sbClock.setOnCheckedChangeListener { _, _ -> onCheckListener!!.onClick(sbClock) }
            executePendingBindings()
        }
    }

}

data class ClockData(
    val id: Int,
    var clockTime: String,
    var weekRepeat: Int,
    var selected: Boolean,
    val openState:Int = if (selected){1}else 0
) : Serializable